package com.datoucai.consumer;

import com.alibaba.fastjson.JSON;
import com.datoucai.dto.CommentInfoDto;
import com.datoucai.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(
        topic = "DEL_COMMENT_TOPIC",
        consumerGroup = "del-comment-remove-cache-consumer-group"
)
@Slf4j
public class DelCommentRemoveCacheConsumer implements RocketMQListener<CommentInfoDto> {


    @Autowired
    private RedissonClient redissonClient;

    @Override
    public void onMessage(CommentInfoDto dto) {
        log.info("删除评论-删除缓存-消费入参:{}", JSON.toJSONString(dto));
        String key = RedisUtils.getCommentKey(dto);
        RBucket<Object> bucket = redissonClient.getBucket(key);
        boolean delete = bucket.delete();
        log.info("删除评论-删除key:{},结果:{}",key,delete);
    }

}